package com.fansl.allround.auth.service;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fansl.allround.common.core.constant.SecurityConstants;
import com.fansl.allround.common.core.constant.enums.OauthTypeEnum;
import com.fansl.allround.common.security.service.AllroundUcenterUser;
import com.fansl.allround.common.security.service.AllroundUserDetailsService;
import com.fansl.allround.ucenter.api.feign.RemoteUcenterUserService;
import com.fansl.allround.ucenter.api.vo.UcenterUserOauthVO;
import com.fansl.allround.common.core.bean.UserInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/7/15 14:27
 */
@Slf4j
@Service
@AllArgsConstructor
public class UcenterUserDetailsServiceImpl implements AllroundUserDetailsService {
    private final RemoteUcenterUserService remoteUserService;

    /**
     * 根据用户名获取用户信息
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return null;
    }

    /**
     * 根据认证类型获取用户信息
     *
     * @param oauthName 认证内容
     * @param oauthType 认证类型
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByOauthType(String oauthName, OauthTypeEnum oauthType) throws UsernameNotFoundException {
        UserInfo result = remoteUserService.info(oauthName, oauthType, SecurityConstants.FROM_IN);
        if (result == null || result.getUser() == null) {
            return null;
        }
        return getUserDetails(result);
    }

    /**
     * 注册用户
     *
     * @param userInfo
     * @return
     */
    @Override
    public UserDetails getUserDetails(UserInfo userInfo) {
        if (userInfo == null || userInfo.getUser() == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        Set<String> dbAuthSet = new HashSet<>();
        if (ArrayUtil.isNotEmpty(userInfo.getRoles())) {
            //获取角色
            Arrays.stream(userInfo.getRoles()).forEach(role -> dbAuthSet.add(SecurityConstants.ROLE + role));
            //获取资源
            dbAuthSet.addAll(Arrays.asList(userInfo.getPermissions()));
        }
        Collection<? extends GrantedAuthority> authorities =
                AuthorityUtils.createAuthorityList(dbAuthSet.toArray(new String[0]));
        UcenterUserOauthVO user = JSONObject.parseObject(JSONObject.toJSONString(userInfo.getUser()),
                UcenterUserOauthVO.class);

        //构造security用户
        AllroundUcenterUser allroundUser = new AllroundUcenterUser(user.getUserId(), user.getNickname(),
                SecurityConstants.BCRYPT + user.getPassword(), authorities, true);
        return allroundUser;
    }
}
